#include "parallel.h"

void swap(double list[],int i,int j) {
    double temp = list[i];
    list[i] = list[j];
    list[j] = temp;
}

void odd_even_sort(double arr[], int len){
	double temp;
	int sorted = 0;
	while (!sorted) {
		sorted = 1;
        #pragma omp parallel for shared(arr, sorted) 
        for (int i = 1; i < len - 1; i += 2) {
            if (arr[i] > arr[i + 1]) {
                swap(arr, i, i + 1);
                sorted = 0;
            }
        }
        #pragma omp parallel for shared(arr) 
        for (int i = 0; i < len - 1; i += 2) {
            if (arr[i] > arr[i + 1]) {
                swap(arr, i, i + 1);
                sorted = 0;
            }
        }
	}
}

int main(int argc, char **argv){
    int n = 20000;
    double arr[n];
    
    for(int i = 0; i < threads_list_num; i++){

        for(int j = 0; j < n ; j++) arr[j] = rand();
        double start, end; 
        int thread = threads[i]; 
        omp_set_num_threads(thread);
        start = omp_get_wtime(); 
        odd_even_sort(arr, n);
        end = omp_get_wtime(); 
        printf("thread %d Work took %f seconds\n",thread, end - start);
    }
}

